/**
 * @param {number} capacity
 */
var LRUCache = function(capacity) {
    this.keyMap = new Map();
    this.capacity = capacity;
    this.keyArr = new Array(this.capacity);
};

LRUCache.prototype.cacheKey = function(key){
    var idx = this.keyArr.indexOf(key);
    if(idx>-1){
        this.keyArr.splice(idx,1);
        this.keyArr.push(key);
    }else{
        if(this.keyArr.length>=this.capacity){
            var delKey = this.keyArr.shift();
            this.keyMap.delete(delKey);
        }
        this.keyArr.push(key);
    }
}

/** 
 * @param {number} key
 * @return {number}
 */
LRUCache.prototype.get = function(key) {
    if(this.keyMap.has(key)){
        this.cacheKey(key);
        return this.keyMap.get(key);
    }else{
        return -1;
    }
};

/** 
 * @param {number} key 
 * @param {number} value
 * @return {void}
 */
LRUCache.prototype.put = function(key, value) {
    this.cacheKey(key);
    this.keyMap.set(key, value);
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * var obj = new LRUCache(capacity)
 * var param_1 = obj.get(key)
 * obj.put(key,value)
 */

var cache = new LRUCache( 2 /* 缓存容量 */ );

cache.put(1, 1);
cache.put(2, 2);
cache.get(1);       // 返回  1
cache.put(3, 3);    // 该操作会使得关键字 2 作废
cache.get(2);       // 返回 -1 (未找到)
cache.put(4, 4);    // 该操作会使得关键字 1 作废
cache.get(1);       // 返回 -1 (未找到)
cache.get(3);       // 返回  3
cache.get(4);       // 返回  4
console.log(cache.keyMap);
console.log(cache.keyArr);